package com.logistics.utils.interceptor;

import com.logistics.utils.configure.Configure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.stream.Collectors;
import java.util.stream.Stream;


@Component
@ConditionalOnProperty(name = {"configure.filter"}, matchIfMissing = true)
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    private AuthorizationInterceptor authorizationInterceptor;

    @Autowired
    private Configure configure;

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(authorizationInterceptor)
                .addPathPatterns(configure.getInterceptor().getPath())
                .excludePathPatterns(Stream.of(configure.getInterceptor()
                        .getExclude().getPaths()).collect(Collectors.toList()));
    }

}
